﻿
using Inovout.Kuajing.Models;
using Inovout.Kuajing.Store.Models;
using Inovout.Kuajing.Store.Web.Models;
using Inovout.Locations;
using Inovout.Memberships.Models;
using Inovout.Organizations.Memberships.Models;
using Inovout.Organizations.Memberships.Services;
using Inovout.Organizations.Models;
using Inovout.Organizations.Services;
using Inovout.People.Models;
using Inovout.Services;
using Inovout.Web.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inovout.Kuajing.Store.Web.Controllers
{

    public class ConsignerController : StoreBaseController<Consigner>
    {
        private IEmployeeService employeeService;
        private IOrganizationUnitService organizationUnitService;
        private ILocationService locationService;
        public ConsignerController(IEmployeeService employeeService, IOrganizationUnitService organizationUnitService,
                                   ILocationService locationService)
        {
            this.employeeService = employeeService;
            this.organizationUnitService = organizationUnitService;
            this.locationService = locationService;
        }

        public override ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ConsignerInfo consignerInfo)
        {
            OrganizationUnit organizationUnit = null;
            if (!consignerInfo.CommpanyCode.Equals(0))
            {
                organizationUnit = organizationUnitService.FindById(consignerInfo.CommpanyCode);
            }
            else
            {
                organizationUnit = new OrganizationUnit();
            }
            organizationUnit.Name = consignerInfo.CommpanyName.Trim();
            Location location = locationService.FindByName("中国");
            if (location == null)
            {
                location = new Location();
                location.ZoneCode = 2;
                location.ScopeCode = 1;
                location.Name = "中国";
                location.Parent = location;
                location.EnglishName = "china";
            }
            organizationUnit.Location = location;
            organizationUnitService.Save(organizationUnit);

            List<PersonInfo> personInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PersonInfo>>(consignerInfo.Persons);
            foreach (PersonInfo item in personInfoList)
            {
                Employee employee = new Employee();
                Person person = new Person();
                Phone phone = new Phone() { Label = PhoneLabel.Mobile };
                person.Name = item.Name;
                phone.Value = item.Phone;
                person.Phones.Add(phone);
                employee.Person = person;
                employee.Organization = organizationUnit;
                employeeService.Save(employee);
            }

            Consigner consignee = new Consigner();
            consignee.OrganizationUnit = organizationUnit;
            consignee.Membership = (Membership)Membership;
            consignee.LoginName = System.Threading.Thread.CurrentPrincipal.Identity.Name;
            IRepositoryService<Consigner> consignerService = ComponentRegistry.Resolve<IRepositoryService<Consigner>>();
            consignerService.Save(consignee);
            return RedirectToAction("UnShippedOrderList", "Order");

        }


    }


}